/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Classifier;

import DataBase.DBConnection;
import Entities.Mail;
import Parser.EmailParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.mail.MessagingException;

/**
 *
 * @author fabio
 */
public class ClassifyMails {

    private String classifyMailsPath = "SpamDataSet/TESTE/";
    private String[] classifyMailsFilenames;

    public void run() throws Exception {
        Classifier classifier = new Classifier("Modelos/");
        ArrayList<Mail> mails = new ArrayList<Mail>();
        File dir = new File(classifyMailsPath);
        classifyMailsFilenames = dir.list();
        System.out.println("A iniciar carregamento de emails...");
        for (int i = 0; i < classifyMailsFilenames.length; i++) {
            Mail mail = new Mail(classifyMailsPath + classifyMailsFilenames[i], -1);
            EmailParser eParser = new EmailParser();
            try {
                System.out.println("-A carregar email numero: " + i);
                mail = eParser.readMail(mail);
                mails.add(mail);
                mail.setSpam(-1);
                DBConnection.insertMail(mail);
            } catch (FileNotFoundException ex) {
                System.out.println(ex.toString());
            } catch (MessagingException ex) {
                System.out.println(ex.toString());
            } catch (IOException ex) {
                System.out.println(ex.toString());
            }
        }
        System.out.println("A classificar mails...");
        ArrayList<Integer> classif = classifier.emailClassifier(mails.size());
        ArrayList<Integer> idList = classifier.getIdList();
        System.out.println("-Classificações de emails:");
        for (int i = 0; i < classif.size() && i < mails.size(); i++) {
            String classifiStr = "";
            System.out.println(classif.get(i));
            if (classif.get(i) == 0) {
                classifiStr = "Não Spam";
            } else {
                classifiStr = "Spam";
            }
            DBConnection.updateSpam(idList.get(i), classif.get(i));
            System.out.println("--Email: " + mails.get(i).getFilename() + "\t\t Classificação: " + classifiStr);
        }
    }
}
